Skip to content

Lock upstream wire contract; harden auth, refresh, and consume idempotency - #4

Merged
hcsolakoglu merged 2 commits into
mainfrom
feat/upstream-contract-lock
Aug 15, 2026
Merged

Lock upstream wire contract; harden auth, refresh, and consume idempotency#4
hcsolakoglu merged 2 commits into
mainfrom
feat/upstream-contract-lock

Conversation

@hcsolakoglu

Copy link
Copy Markdown
Owner

Summary

Brings codex-reset to verified compatibility with current openai/codex (analysis window 2026-07-13 → 2026-08-16, HEAD b3cc21737) by pinning the wire contract in tests rather than hand-copied constants, and remediates the defects an adversarial review of that analysis exposed. Includes a second adversarial review pass over the implementation itself; all its actionable findings are fixed here (one rejected as noise, documented below).

Contract lock (Phase 1)

  • Injectable HTTP transport (src/core/http.ts) + CODEX_RESET_BASE_URL override. Production behavior unchanged — now regression-tested against a live localhost socket (test/http-transport.test.ts).
  • Upstream manifest (tools/extract-upstream-manifest.mjstest/fixtures/upstream-manifest.json): generated from 11 Rust files in a pinned checkout — endpoints, consume request/response fields, snake_case result codes, window fields, plan display names, JWT claim namespaces, AuthDotJson schema, headers, refresh grant, PAT whoami. Never hand-edited.
  • Contract tests (test/upstream-contract.test.ts) assert the tool's request boundary against the manifest, and live re-extract + deep-compare when a checkout is present.

Defect hardening (Phase 2)

Defect Fix
Fresh idempotency key per consume → double-spend on retry of an ambiguous send Key persisted pre-send per account, reused across CLI invocations (timeout / malformed 2xx / 5xx), warning when a different credit or >24h record forces a fresh key
PAT accounts silently dropped Hydrated via upstream whoami endpoint; bearer = PAT
FedRAMP claim never read X-OpenAI-Fedramp: true emitted for FedRAMP accounts
No token refresh Proactive (JWT exp) + reactive (401 → refresh → retry once); rotation persisted atomically; upstream-parity failure messages surfaced in the 401 hint
Account-id resolution deviated from upstream tokens.account_id → claim → organizations (last-resort discovery only)
Plan label gaps Official KnownPlan::display_name() map (ent26 → "Enterprise", enterprise_cbp_automation → "Enterprise (Automation)", …) with graceful unknown-value fallback
Sparse error handling 401/403/429(+Retry-After)/5xx/HTML/empty/oversized/network matrix with actionable messages

Drift prevention (Phase 3)

Weekly non-blocking upstream-drift workflow: regenerates the manifest from upstream HEAD, uploads the drift diff, opens/updates an issue on drift — and opens an upstream-drift-broken issue if the check itself fails, so drift tracking cannot die silently.

Verification

  • npm ci && build && typecheck && lint && test123/123 green (baseline was 52), exit 0
  • Wire behavior probe-verified through the real transport against a live socket (headers, consume body, 401→refresh→retry, 15s timeout)
  • codex-auth producer audit (A1 gate) passed: parser matches the emitted AuthDotJson/registry.json schema
  • Second adversarial review (self-probes + independent fresh-context reviewer): 12 findings fixed, 1 skipped as noise (PAT discovery whoami latency — matches upstream semantics, near-zero affected population)

Not in scope (unchanged)

Zero-dependency constraint, spend-control display (feature, deferred), keyring-backed credential storage (documented file-only), no live reset execution against real credits.

…me idempotency

Verify codex-reset against current openai/codex (analyzed 2026-07-13..2026-08-16)
by pinning the wire contract in tests instead of hand-copied constants, and fix
the auth/routing/retry defects an adversarial review of that analysis exposed.

Contract lock:
- injectable HTTP transport (core/http.ts) + CODEX_RESET_BASE_URL override;
  production path unchanged and now regression-tested against a live socket
- tools/extract-upstream-manifest.mjs generates the semantic manifest from 11
  upstream Rust files; test/upstream-contract.test.ts asserts endpoints, consume
  request/response, snake_case codes, window fields, plan display names, JWT
  claims, auth-file schema, headers, refresh and whoami contracts against it
  and live-diffs a re-extraction when a checkout is present

Defect hardening:
- idempotent consume: persist redeem_request_id before the POST, reuse it across
  invocations on ambiguous outcomes (timeout / malformed 2xx / 5xx), warn when a
  different credit or a >24h record forces a fresh key (core/idempotency.ts)
- FedRAMP: read chatgpt_account_is_fedramp, send X-OpenAI-Fedramp: true
- PAT accounts: hydrate via the upstream whoami endpoint instead of dropping
- token refresh: proactive (exp claim) + reactive (401 -> refresh -> retry once),
  rotation persisted atomically, upstream-parity failure messages surfaced
- account-id precedence matches upstream (tokens.account_id -> claim -> orgs)
- plan labels from KnownPlan::display_name() (ent26 -> Enterprise, etc.)
- error matrix: 403/429+Retry-After/5xx/HTML/empty/oversized/network messages
- auth-mode matrix: apikey/agentIdentity/bedrock/token-less files skipped with
  warnings instead of silently; organizations + profile.email fallbacks

Tests: 52 -> 123, all green; production transport covered end to end.
Weekly (and manual) job regenerates the upstream manifest from a sparse
openai/codex clone, uploads the drift diff as an artifact, and opens or
updates an issue on drift. When the check itself fails (extractor crash on
an upstream refactor), a separate always()+failure() step opens an
upstream-drift-broken issue so drift tracking cannot die silently. The job
is continue-on-error and never runs on pull requests.

README: supported auth modes table (chatgpt / personalAccessToken via whoami /
apikey+agent+bedrock skipped with warnings), file-storage-only credential
model (keyring not read), token refresh + rotation behavior, FedRAMP
routing header, precise idempotent-redemption guarantee with its limits,
env overrides, and manifest maintenance instructions.
@hcsolakoglu
hcsolakoglu merged commit 959b63a into main Aug 15, 2026
6 checks passed
@hcsolakoglu
hcsolakoglu deleted the feat/upstream-contract-lock branch August 15, 2026 23:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant